package com.inspur.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.inspur.common.annotation.Log;
import com.inspur.common.core.controller.BaseController;
import com.inspur.common.core.domain.AjaxResult;
import com.inspur.common.core.page.TableDataInfo;
import com.inspur.common.enums.BusinessType;
import com.inspur.common.utils.poi.ExcelUtil;
import com.inspur.system.domain.PmAuth;
import com.inspur.system.domain.PmRole;
import com.inspur.system.service.IPmAuthService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 项目权限Controller
 * 
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/system/auth")
public class PmAuthController extends BaseController
{
    @Autowired
    private IPmAuthService pmAuthService;

    /**
     * 查询项目权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:auth:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmAuth pmAuth)
    {
        startPage();
        List<PmAuth> list = pmAuthService.selectPmAuthList(pmAuth);
        return getDataTable(list);
    }

    /**
     * 导出项目权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:auth:export')")
    @Log(title = "项目权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmAuth pmAuth)
    {
        List<PmAuth> list = pmAuthService.selectPmAuthList(pmAuth);
        ExcelUtil<PmAuth> util = new ExcelUtil<PmAuth>(PmAuth.class);
        util.exportExcel(response, list, "项目权限数据");
    }

    /**
     * 获取项目权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:auth:query')")
    @GetMapping(value = "/{authId}")
    public AjaxResult getInfo(@PathVariable("authId") String authId)
    {
        return success(pmAuthService.selectPmAuthByAuthId(authId));
    }

    /**
     * 新增项目权限
     */
    @PreAuthorize("@ss.hasPermi('system:auth:add')")
    @Log(title = "项目权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmAuth pmAuth)
    {
        return toAjax(pmAuthService.insertPmAuth(pmAuth));
    }

    /**
     * 修改项目权限
     */
    @PreAuthorize("@ss.hasPermi('system:auth:edit')")
    @Log(title = "项目权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmAuth pmAuth)
    {
        return toAjax(pmAuthService.updatePmAuth(pmAuth));
    }

    /**
     * 删除项目权限
     */
    @PreAuthorize("@ss.hasPermi('system:auth:remove')")
    @Log(title = "项目权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{authIds}")
    public AjaxResult remove(@PathVariable String[] authIds)
    {
        return toAjax(pmAuthService.deletePmAuthByAuthIds(authIds));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect")
    public AjaxResult roleMenuTreeselect(@RequestParam(required = false) Long userId,
                                         @RequestParam(required = false) String cProNo, @RequestParam(required = false) String roleId)
    {
        List<PmAuth> menus = pmAuthService.selectMenuList(userId,cProNo);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", pmAuthService.selectMenuListByRoleId(roleId));
        ajax.put("menus", pmAuthService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/editRoleAuth")
    public AjaxResult edit(@Validated @RequestBody PmRole role)
    {
        role.setUpdateBy(getUsername());
        return success(pmAuthService.updateRole(role));
    }

}
